﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DTO
{
    public class Basket
    {
        List<Product> ListPro = new List<Product>();


        public void addProduct(Product product)
        {
            // Product is exists --> Update so luong
            if (this.isExists(product.Id))
            {
                if (product.Quantity == 0)
                {
                    this.delProduct(product.Id);
                }
                else
                {
                    ListPro[FindProductID(product.Id)].Quantity = product.Quantity;
                    ListPro[FindProductID(product.Id)].Money = product.Quantity * product.Price;
                }
                
            }
            else
            {
                ListPro.Add(product);
            }
        }
        /*
            Have to Indexed before just apply this function !!!
            ==> So applying Bubble sort  after every adding product in List
        */
        private int BinSearchProductById(int Id)
        {
            int left=0, right=ListPro.Count;
            while (left <= right)
            {
                int middle = (left + right) / 2;
                if (ListPro[middle].Id == Id)
                    return middle;
                else if (ListPro[middle].Id > Id)
                    right = middle - 1;
                else
                    left = middle + 1;
            }
            return -1;
        }
        // Normal search - reuturn index number of Product in List
        private int FindProductID(int Id)
        {
            for (int i = 0; i < ListPro.Count; i++)
                if (ListPro[i].Id == Id)
                    return i;
            return -1;
        }
        // Check Id of Product exists whether ?
        private bool isExists(int Id)
        {
            for (int i = 0; i < ListPro.Count; i++)
                if (ListPro[i].Id == Id)
                    return true;
            return false;

        }
        public void delProduct(int Id)
        {
            ListPro.RemoveAt(this.FindProductID(Id));
        }
        public string viewHeader()
        {
            string strHTMLHeaderTable = "";
            strHTMLHeaderTable += "<tr><td>STT</td><td>Mã hàng</td><td>Tên hàng</td><td>Số lượng</td><td>Đơn vị tính</td><td>Đơn giá</td><td>Thành tiền</td></tr>";
            return strHTMLHeaderTable;
        }
        public string toHTMLString()
        {
            string strHTML = "<table cellpadding=\"1px\" cellspacing=\"1px\" border=\"1px\">";
            strHTML += viewHeader();
            for (int i = 0; i < ListPro.Count; i++)
            {
                strHTML += "<tr>";
                strHTML += "<td>" + (i+1).ToString()        + "</td>";
                strHTML += "<td>" + ListPro[i].Id       + "</td>";
                strHTML += "<td>" + ListPro[i].Name     + "</td>";
                strHTML += "<td>" + ListPro[i].Quantity + "</td>";
                strHTML += "<td>" + ListPro[i].Unit     + "</td>";
                strHTML += "<td>" + ListPro[i].Price    + "</td>";
                strHTML += "<td>" + ListPro[i].Money    + "</td>";
                strHTML += "<td><a href='#' onclick='return delProduct(" + ListPro[i].Id + ")'>Xóa</a></td>";
                strHTML += "</tr>";
            }

            return strHTML += "</table>";

        }

    }
}
